//
//  YYT_LeftView.m
//  AudioVisualFeast
//
//  Created by 温晓宇 on 16/1/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "YYT_LeftView.h"
#import "YYT_UserTableViewCell.h"
#import "YYT_LoginTableViewCell.h"
#import <UIImageView+WebCache.h>
@interface YYT_LeftView ()
<
    UITableViewDataSource,
    UITableViewDelegate,
    YYT_UserTableViewCellDelegate,
    YYT_LoginTableViewCellDelegate
>




@end

@implementation YYT_LeftView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self create];
    }
    return self;
}
- (void)create{
    self.myTableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.separatorStyle = NO;
    _myTableView.backgroundColor = BLACK_COLOR;
    [_myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [_myTableView registerClass:[YYT_UserTableViewCell class] forCellReuseIdentifier:@"user"];
    [_myTableView registerClass:[YYT_LoginTableViewCell class] forCellReuseIdentifier:@"login"];
    [self addSubview:_myTableView];
}
// 返回高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 110;
    }else if (indexPath.row == 4) {
        return 50;
    }else {
        return 50;
    }
    return 0;
    
}
// 个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 9;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (indexPath.row == 0) {
        BmobUser *bUser = [BmobUser getCurrentUser];
        if (bUser) {
            YYT_LoginTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"login"];
            cell.userLabel.text = bUser.username;
            cell.delegate = self;
            cell.backgroundColor = BLACK_COLOR;
            return cell;
        }else{
            YYT_UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"user"];
            cell.delegate = self;
            // 点击cell的颜色
            //        cell.backgroundColor = [UIColor colorWithRed:100 / 255.0 green:100 / 255.0 blue:100 / 255.0 alpha:1];
            cell.backgroundColor = BLACK_COLOR;
            return cell;
        }
    }else if (indexPath.row == 1) {
        [self initTableCell:cell Text:@"首页" imageName:@"shouye"];
    }else if (indexPath.row == 2) {
        [self initTableCell:cell Text:@"频道" imageName:@"pindao"];
    }else if (indexPath.row == 3){
        [self initTableCell:cell Text:@"悦单" imageName:@"vbang"];
    }else if (indexPath.row == 4){
        cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
        [self initTableCell:cell Text:nil imageName:nil];
    }else if (indexPath.row == 5){
        [self initTableCell:cell Text:@"分享MV" imageName:@"fenxiang"];
    }else if (indexPath.row == 6){
        [self initTableCell:cell Text:@"下载列表" imageName:@"wodeshoucang"];
    }else if (indexPath.row == 7){
        [self initTableCell:cell Text:@"播放记录" imageName:@"bofanglishi"];
    }else if (indexPath.row == 8){
        NSInteger size = [[SDImageCache sharedImageCache] getSize];
        float sizeMB = size / 1024.0 / 1024.0;
        [self initTableCell:cell Text:[NSString stringWithFormat:@"清除缓存         %0.2fMB", sizeMB] imageName:@"qingchuhuancun"];
    }
    // 去除cell的背景颜色
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}





- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    if (indexPath.row == 1)
    {
        [self.delegate TheOfficial];
    }
    else if (indexPath.row == 2)
    {
        [self.delegate pindao];
    }
    else if (indexPath.row == 3)
    {
        [self.delegate yuedan];
    }
    else if (indexPath.row == 5)
    {
        
    }
    else if (indexPath.row == 6)
    {
        [self.delegate xiazai];
    }
    else if (indexPath.row == 7)
    {
        [self.delegate history];
    }
    else if (indexPath.row == 8)
    {
        [self.delegate qingchu];        
    }
    

}
- (void)regisView{
    [self.delegate regisPopView];
}
- (void)loginView{
    [self.delegate loginPopView];
    NSLog(@"登录2");
}

-(void)logOut{
    [self.delegate logOut];
}


//cellForRow显示封装
- (void)initTableCell:(UITableViewCell *)cell Text:(NSString *)text imageName:(NSString *)name{
    cell.textLabel.text = text;
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.imageView.image = [UIImage imageNamed:name];
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
